package com.boyinet.imesh2json.model

import com.boyinet.imesh2json.bean.Template
import com.boyinet.imesh2json.common.IdGenerator
import javafx.beans.property.*

class TemplateModel {

    var id: LongProperty = SimpleLongProperty(-1)
    var name: StringProperty = SimpleStringProperty(null)
    var sn: StringProperty = SimpleStringProperty(null)
    var signalStrength: StringProperty = SimpleStringProperty(null)
    var powerSource: StringProperty = SimpleStringProperty(null)
    var powerSupply: StringProperty = SimpleStringProperty(null)
    var batteryPower: StringProperty = SimpleStringProperty(null)
    var alarmBit: StringProperty = SimpleStringProperty(null)
    var originalValue: BooleanProperty = SimpleBooleanProperty(false)

    var fields: Property<String> = SimpleStringProperty(null)
    var decimalPlaces = SimpleStringProperty(null)
    var unit: StringProperty = SimpleStringProperty(null)
    var sensorType = SimpleStringProperty(null)

    var isDelete = false

    fun init() {
        fill(Template(IdGenerator.instance.nextId()))
    }

    fun fill(template: Template) {
        id.value = template.id
        name.value = template.name
        sn.value = template.sn
        signalStrength.value = template.signalStrength
        powerSource.value = template.powerSource
        powerSupply.value = template.powerSupply
        batteryPower.value = template.batteryPower
        alarmBit.value = template.alarmStatus
        originalValue.value = template.originalValue

        fields.value = template.fields
        decimalPlaces.value = template.decimalPlaces
        unit.value = template.unit
        sensorType.value = template.sensorType
    }

    fun export(): Template {
        val current = this
        return Template(current.id.value).apply {
            name = current.name.value
            sn = current.sn.value
            signalStrength = current.signalStrength.value
            powerSource = current.powerSource.value
            powerSupply = current.powerSupply.value
            batteryPower = current.batteryPower.value
            alarmStatus = current.alarmBit.value
            originalValue = current.originalValue.value

            fields = current.fields.value
            decimalPlaces = current.decimalPlaces.value
            unit = current.unit.value
            sensorType = current.sensorType.value
        }
    }

}